# coding=utf8
# 全世界最帅的帅哥
from comment.utils.serializers import BasePaginateSerializer, BaseListSerializer
from datetime import datetime


class InvestRecordPaginateSerializer(BasePaginateSerializer):
    # 投资记录列表的分页序列化
    def get_object(self, obj):
        return {
            "pId": obj.pId,  # 主键
            "planName": obj.product.productName,  # 产品名称
            "investAmount": float(obj.pAmount),  # 金额
            "yearRate": float(obj.pEarnings),  # 收益率
            "totalIncome": float(obj.pMonthInterest),  # 每月盈取利息
            "monthIncome": float(obj.pMonthlyExtractInterest),  # 每月提取利息
            "dealDate": obj.pBeginDate.strftime("%Y-%m-%d"),  # 加入日期
            "period": obj.pDeadline / 30,  # 选择期限
            "status": obj.pStatus  # 状态
        }


class DecordRecordPaginateSerializer(BasePaginateSerializer):
    # 交易记录列表的分页序列化
    def get_object(self, obj):
        return {
            "dealDate": obj.aDate.strftime("%Y-%m-%d"),  # 交易时间
            "dealType": obj.aType,  # 交易类型
            "description": obj.aDescription,  # 交易详情
            "dealAmount": float(obj.aAmount),  # 金额
            "afterMoney": float(obj.aAfterTradingMoney),  # 交易后金额
            "dealStatus": "交易成功" if obj.aTransferStatus else "交易失败"
        }


class DebtPaginateSerializer(BasePaginateSerializer):
    # 债券的分页序列化
    def get_object(self, obj):
        return {
            "debtNo": obj.debtNo,
            "loanNo": obj.loanNo,
            "loanStartDate": obj.loanStartDate.strftime("%Y-%m-%d"),
            "repaymentDate": obj.repaymentDate,
            "debtYearRate": obj.debtYearRate,
            "debtMoney": float(obj.debtMoney),
            "debtOriginalMoney": float(obj.debtTransferredMoney),
            "matchedStatus": obj.matchedStatus,
            "matchedMoney": float(obj.matchedMoney),
            "debtStatus": obj.matchedStatusName
        }


class LoanPaginateSerializer(BasePaginateSerializer):
    # 借款的分页序列化
    def get_object(self, obj):
        return {
            "loanApplyId": obj.id,
            "loanName": obj.lName,
            "applyTime": obj.applyDate.strftime("%Y-%m-%d"),
            "amount": float(obj.loanAmount),
            "duration": obj.duration,
            "loanRate": float(obj.lRate),
            "repayDay": obj.lRepayDay,
            "repayType": obj.lRepayType,
            "status": obj.status,
            "debtMatchedStatus": obj.debt_info.matchedStatus if obj.debt_info else "",
            "debtId": obj.debt_info.id if obj.debt_info else ""
        }


class RepayPlanSerializer(BaseListSerializer):
    # 还款计划的序列化
    def to_dict(self):
        repay_list = []
        for obj in self.data_list:
            repay_list.append(
                {
                    'id': obj.id,
                    'currentTerm': obj.currentTerm,
                    # 'receivableDate': obj.receivableDate.strftime("%Y-%m-%d"),
                    "receivableMoney": float(obj.receivableMoney),
                    'isReturned': obj.isReturned,
                }
            )
        return repay_list


class AllMatchedPaginateSerializer(BasePaginateSerializer):
    # 所有待匹配资金和 已经匹配的资金的序列化
    def get_object(self, obj):
        return {
            "weight": 0,
            "username": obj.investRecord.user.username,
            "investRecordNum": obj.investRecord.pSerialNo,
            "productName": obj.investRecord.pProductName,
            "investDate": obj.investRecord.pDate.strftime("%Y-%m-%d"),
            "period": obj.investRecord.pDeadline,
            "notMatchedMoney": float(obj.fNotMatchedMoney),
            "matchStatus": obj.matchedStatus
        }


class ExpectedReturnPaginateSerializer(BasePaginateSerializer):
    # 预期收益的序列化类
    def get_object(self, obj):
        return {
            "return_id": obj.id,
            "userId": obj.userId,
            "productName": obj.product.productName,
            "investRecordId": obj.investRecord,
            "expectedDate": obj.expectedDate.strftime("%Y-%m-%d"),
            'expectedStamp': datetime.timestamp(datetime(obj.expectedDate.date().year, obj.expectedDate.date().month,
                                                         obj.expectedDate.date().day)),
            "returnMoney": float(obj.expectedMoney)
        }


class MatchedResultPaginateSerializer(BasePaginateSerializer):
    # 匹配结果分页的序列化
    def get_object(self, obj):
        return {
            "userId": obj.userId,
            "debtId": obj.debtId,
            "investId": obj.investId,
            "transNo": obj.transferSerialNo,  # 交易流水号
            "money": float(obj.purchaseMoney),  # 购买金额（匹配金额）
            "matchDate": obj.matchDate.strftime("%Y-%m-%d %H:%M:%S")
        }